import { ListBase } from 'src/components/list_base_abs';
import { Component, Emit, Prop, Provide } from 'vue-property-decorator'

import { AuditTConst } from "src/consts/audit_template_const";
import { updateSelection } from 'src/utils/list_utils';
import OperatorHandler from 'src/views/audit_template/operator_handler';
import ListDemoHandler from 'src/views/audit_template/list_handler';

@Component({})
export default class AuditTemplate extends ListBase {
    operatorData: OperatorHandler;
    listHandler: ListDemoHandler;
    isReset: Boolean = false;
    isQiYong: Boolean = false;
    isTingYong: Boolean = false;
    mainData: any;
    queryData: any;
    feature_code: String;

    initData(that: AuditTemplate) {
        that.myState = this.$store.state.auditTemplate;
        that.selection = [];
        that.operatorData = new OperatorHandler();
        that.listHandler = new ListDemoHandler();
        that.operatorData.init(this);

        that.mainData = {};
        that.queryData = {};
        that.feature_code = Config.featureCode + 'audit.model.AuditFlowSettingView'
    }

    getListData(): void {
        WLDispatch(AuditTConst.DispatchAuditTList, this.myState);
    }

    mounted(): void {
        this.getListData();
    }

    doQuery(obj: any): void {
        var obj: any = {}
        for (let key in this.mainData) {
            if (this.mainData[key]) {
                obj[key] = this.mainData[key]
            }
        }


        this.queryData = obj
        obj["curr_page"] = this.myState.curr_page
        obj["page_size"] = this.myState.page_size
        WLDispatch(AuditTConst.DispatchAuditTList, obj);
    }

    sizeChange(size: number): void {
        let senData: any = this.queryData
        senData["curr_page"] = this.myState.curr_page
        senData["page_size"] = size
        WLDispatch(AuditTConst.DispatchAuditTList, senData);
    }

    pageChange(page: number): void {
        let senData: any = this.queryData
        senData["curr_page"] = page
        senData["page_size"] = this.myState.page_size
        WLDispatch(AuditTConst.DispatchAuditTList, senData);
    }

    handleSelectionChange(list: any[]): void {
        updateSelection(this.selection, list);
        this.handleBtnShow(this.selection)
        console.log("this selection", this.selection, this.selection.length);
    }

    handleBtnShow(data: any): void {
        if (data.length > 0) {
            if (data[0].status == "qiyong") {
                this.isTingYong = true;
                this.isQiYong = false;
            } else if (data[0].status == "tingyong") {
                this.isQiYong = true;
                this.isTingYong = false;
            }
            this.isReset = true
            if (data.length > 1) {
                for (let i = 0; i < data.length; i++) {
                    if (i > 0) {
                        if (data[i].status == data[i - 1].status) {
                            if (data[i].status == "qiyong") {
                                this.isTingYong = true;
                                this.isQiYong = false;
                            } else if (data[i].status == "tingyong") {
                                this.isQiYong = true;
                                this.isTingYong = false;
                            }
                        } else {
                            this.isQiYong = false;
                            this.isTingYong = false;
                            return
                        }
                    }
                }
            }

        } else {
            this.isReset = false
            this.isQiYong = false;
            this.isTingYong = false;
        }
    }

    rowClassName(data: any): string {
        if (data.row.selected) {
            return 'selected_row';
        }
        return '';
    }

    doNew(): void {
        if (this.operatorData && this.operatorData.doNew) {
            this.operatorData.doNew();
        } else {
            this.$message.warning("todo doNew 方法");
        }
    }

    doQiYong(): void {
        this.$confirm('是否确定启用审核模版?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            if (this.operatorData && this.operatorData.doQiYong) {
                for (let i = 0; i < this.selection.length; i++) {
                    let sendData: any = {};
                    sendData["id"] = this.selection[i].id
                    sendData["status"] = "qiyong"
                    sendData["type"] = "qy"
                    sendData["flag"] = false
                    if (i == this.selection.length - 1) { sendData["flag"] = true }
                    this.operatorData.doQiYong(sendData);
                }
            }
        }).catch(() => { })
    }

    doTingYong(): void {
        this.$confirm('是否确定停用审核模版?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            if (this.operatorData && this.operatorData.doQiYong) {
                for (let i = 0; i < this.selection.length; i++) {
                    let sendData: any = {};
                    sendData["id"] = this.selection[i].id
                    sendData["status"] = "tingyong"
                    sendData["type"] = "ty"
                    sendData["flag"] = false
                    if (i == this.selection.length - 1) { sendData["flag"] = true }
                    this.operatorData.doQiYong(sendData);
                }
            }
        }).catch(() => { })
    }

    rowEdit(ind: number, row: DemoData): void {
        if (this.listHandler && this.listHandler.rowEdit) {
            this.listHandler.rowEdit(ind, row);
        } else {
            console.warn("todo 初始化和实现行内容编辑");
        }
    }

}